package com.gosophia.dataDictionary.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.gosophia.commons.persistence.BaseDao;
import com.gosophia.dataDictionary.entity.DataValue;

/**
 * 数据字典管理DAO接口的实现类--数据值部分DataValueDAOImpl
 * 
 * @author wangnana
 * @date 2010-8-24
 */
@Repository("dataValueDAO")
public class DataValueDAO extends BaseDao<DataValue, Long> {

    /**
     * 根据词条Id和key获取数据值
     * 
     * @param entryId
     * @param key
     * @return
     */
    public DataValue getDataValueByEntryIdAndKey(Long entryId, String key) {
        DataValue dataValue = this.getUniqueByNamedQuery(
                "DataDictionaryService.findDataValueByEntryIdAndKey", entryId,
                key);
        return dataValue;
    }

    /**
     * 根据词条Id获取词条下启用的数据值
     * 
     * @param page
     * @param entryId
     * @return
     */
    public List<DataValue> getEnabledDataValueByEntryId(Long entryId) {
        List<DataValue> dataValues = this.getListByNamedQuery(
                "DataDictionaryService.findEnabledDataValueByEntryId", entryId);
        return dataValues;
    }

    /**
     * 根据词条Id查找相同value值或key值的数据值
     * 
     * @param value
     * @param key
     * @param entryId
     * @return
     */
    public List<DataValue> getSameValueOrKeyByEntryId(String value, String key,
            Long entryId) {
        List<DataValue> dataValues = this.getListByNamedQuery(
                "DataDictionaryService.findSameValueOrKeyByEntryId", entryId,
                value, key);
        return dataValues;
    }

    /**
     * 根据词条Id查找除数据值外的具有相同value值或key值的数据值（更新时使用）
     * 
     * @param value
     * @param key
     * @param entryId
     * @param dataValueId
     * @return
     */
    public List<DataValue> getSameValueOrKeyByEntryIdExceptByDataValueId(
            String value, String key, Long entryId, Long dataValueId) {
        List<DataValue> dataValues = this
                .getListByNamedQuery(
                        "DataDictionaryService.findSameValueOrKeyByEntryIdExceptByDataValueId",
                        entryId, value, key, dataValueId);
        return dataValues;
    }

    /**
     * 删除数据值
     * 
     * @param dataValueId
     * @param version
     */
    public void deleteDataValueByIdAndVersion(Long dataValueId, Long version) {
        this.delete(dataValueId, version);
        this.flush();
    }

    /**
     * 通过词条名称和Key值查找数据值
     * 
     * @param entryName
     * @param key
     * @return
     */
    public DataValue getDataValueByEntryNameAndKey(String entryName, String key) {
        return this.getUniqueByNamedQuery(
                "DataDictionaryService.findDataValueByEntryNameAndKey",
                entryName, key);
    }

    /**
     * 通过词条名称查找启用的数据值
     * 
     * @param entryName
     * @return
     */
    public List<DataValue> getEnabledDataValueByEntryName(String entryName) {
        return this.getListByNamedQuery(
                "DataDictionaryService.findEnabledDataValueByEntryName",
                entryName);
    }

    /**
     * 查询当前词条下，排序号大于等于toIndex的数据值
     * @param toIndex
     * @param entryId
     * @return
     */
    public List<DataValue> getDataValueByIndexNotLessThanToId(int toIndex,
            Long entryId) {
        return this.getListByNamedQuery(
                "DataDictionaryService.findDataValueByIndexNotLessThanToId",
                toIndex, entryId);
    }

    /**
     * 查询当前词条下，排序号大于fromIndex小于toIndex的数据值(上移时使用)
     * @param fromIndex
     * @param toIndex
     * @param entryId
     * @return
     */
    public List<DataValue> getDataValueBetweenIdMoveUp(int fromIndex,
            int toIndex, Long entryId) {
        return this.getListByNamedQuery(
                "DataDictionaryService.findDataValueBetweenIdMoveUp",
                fromIndex, toIndex, entryId);
    }

    /**
     * 查询当前词条下，排序号大于等于toIndex小于fromIndex的数据值(下移时使用)
     * @param fromIndex
     * @param toIndex
     * @param entryId
     * @return
     */
    public List<DataValue> getDataValueBetweenIdMoveDown(int fromIndex,
            int toIndex, Long entryId) {
        return this.getListByNamedQuery(
                "DataDictionaryService.findDataValueBetweenIdMoveDown",
                 toIndex,fromIndex, entryId);

    }
}

// /:~
